package xyz.suancaiyu.chapter13.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import xyz.suancaiyu.chapter13.entity.Blog;
import xyz.suancaiyu.chapter13.service.impl.BlogService;

import java.util.List;

/**
 * @author puruidong
 * @version 2019/1/17 22:03
 */
@RestController
@RequestMapping("/blogs")
public class BlogController {

    @Autowired
    BlogService blogService;


    @GetMapping
    public ModelAndView list(Model model) {
        List<Blog> list = blogService.getBlogs();
        model.addAttribute("blogsList", list);
        return new ModelAndView("blogs/list", "blogModel", model);
    }


    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    @GetMapping(value="/{id}/deletion")
    public ModelAndView delete(@PathVariable("id") Long id,Model model){
        blogService.deleteBlog(id);
        model.addAttribute("blogsList",blogService.getBlogs());
        return new ModelAndView("blogs/list","blogModel",model);
    }



}
